package com.jzwl.common.upload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Properties;
import java.util.UUID;
import java.util.Vector;

import org.springframework.web.multipart.MultipartFile;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class FtpJSchUtil {

	private static ChannelSftp sftp = null;

	//账号

	private static String user = "root";

	//主机ip

	private static String host = "112.125.90.162";

	//密码

	private static String password = "NCgfkj2018";

	//端口

	private static int port = 22;

	//上传地址

	private static String directory = "/www/wwwroot/NCgeifet";

	//下载目录

	private static String saveFile = "D:\\VMware\\XuNiJi\\imgs";

	public static FtpJSchUtil getConnect() {

		FtpJSchUtil ftp = new FtpJSchUtil();

		try {

			JSch jsch = new JSch();

			//获取sshSession  账号-ip-端口

			Session sshSession = jsch.getSession(user, host, port);

			//添加密码

			sshSession.setPassword(password);

			Properties sshConfig = new Properties();

			//严格主机密钥检查

			sshConfig.put("StrictHostKeyChecking", "no");

			sshSession.setConfig(sshConfig);

			//开启sshSession链接

			sshSession.connect();

			//获取sftp通道

			Channel channel = sshSession.openChannel("sftp");

			//开启

			channel.connect();

			sftp = (ChannelSftp) channel;

		} catch (Exception e) {

			e.printStackTrace();

		}

		return ftp;

	}

	/**

	 *

	 * @param uploadFile 上传文件的路径

	 * @return 服务器上文件名

	 */

	public static String upload(String uploadFile) {

		File file = null;

		String fileName = null;

		try {

			sftp.cd(directory);

			file = new File(uploadFile);

			//获取随机文件名

			fileName = UUID.randomUUID().toString() + file.getName().substring(file.getName().length() - 5);

			//文件名是 随机数加文件名的后5位

			sftp.put(new FileInputStream(file), fileName);

		} catch (Exception e) {

			e.printStackTrace();

		}

		return file == null ? null : fileName;

	}

	public static String upload(MultipartFile img) {

		String fileName = null;

		try {
			getConnect();
			sftp.cd(directory);

			//获取随机文件名
			InputStream inStream = img.getInputStream();
			fileName = URLEncoder.encode(img.getOriginalFilename(), "utf-8");
			fileName = 123 + fileName;

			//文件名是 随机数加文件名的后5位

			sftp.put(inStream, "9999.png");

		} catch (Exception e) {

			e.printStackTrace();

		}

		return fileName;

	}

	/**
	 * 关闭通道
	 * @Date 2019年5月27日
	 * @author gyp(这里描述每个参数,如果有返回值描述返回值,如果有异常描述异常)
	 */
	public void close() {
		try {
			if (sftp != null) {
				if (sftp.isConnected()) {
					sftp.disconnect();
				}
			}
		} catch (Exception e) {

		}
	}

	public void download(String downloadFileName) {

		try {

			sftp.cd(directory);

			File file = new File(saveFile);

			sftp.get(downloadFileName, new FileOutputStream(file));

		} catch (Exception e) {

			e.printStackTrace();

		}

	}

	/**

	 * 删除文件

	 *

	 * @param deleteFile

	 */

	public void delete(String deleteFile) {

		try {

			sftp.cd(directory);

			sftp.rm(deleteFile);

		} catch (Exception e) {

			e.printStackTrace();

		}

	}

	/**

	 * 列出目录下的文件

	 *

	 * @param directory

	 *            要列出的目录

	 * @return

	 * @throws SftpException

	 */

	public Vector listFiles(String directory)

	throws SftpException {

		return sftp.ls(directory);

	}

	public static void main(String[] args) {
		try {
			FtpJSchUtil ftp = FtpJSchUtil.getConnect();
			System.out.println(ftp.listFiles(FtpJSchUtil.directory).size());
			String path = ftp.upload("F://8.png");
			System.out.println("**********************:");
			System.out.println(ftp.listFiles(FtpJSchUtil.directory).size());
		} catch (SftpException e) {
			e.printStackTrace();
		}
	}

}